package com.lc.afbx.fragment;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.activity.InsuranceSearchCarResultActivity;
import com.lc.afbx.activity.InsuranceSearchLifeResultActivity;
import com.lc.afbx.activity.LoginActivity;
import com.lc.afbx.conn.GetApiLiPeiResult;
import com.lc.afbx.conn.GetApiLiPeiSearch;
import com.lc.afbx.dialog.MyAlertDialog;
import com.lc.afbx.util.MyToast;
import com.lc.afbx.util.PrefrenceConfig;
import com.lc.afbx.util.Validator;
import com.zcx.helper.activity.AppV4Fragment;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilNetWord;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/12.
 */
public class LiPeiSerachFragment extends AppV4Fragment implements View.OnClickListener {


    //这是公司的list，popupwindow 的数据源
    private List<String> companyList = new ArrayList<>();
    //这是类型的list,popupwindow 的数据源
    private List<String> tpyeList = new ArrayList<>();
    //这个是请求返回的list
    private List<GetApiLiPeiSearch.Data>  mDataList = new ArrayList<>();

    @BoundView(R.id.company_tv) private TextView company_tv;
    @BoundView(R.id.search_type_tv) private TextView search_type_tv;
    @BoundView(R.id.name_et) private EditText name_et;
    @BoundView(R.id.baodan_num_et) private EditText baodan_num_et;
    @BoundView(R.id.idcard_num_et) private EditText idcard_num_et;
    @BoundView(R.id.driver_num_et) private EditText driver_num_et;
    @BoundView(R.id.phone_num_et) private EditText phone_num_et;
    @BoundView(R.id.driver_rl) private RelativeLayout driver_rl;
    @BoundView(R.id.search_bt) private Button search_bt;

//    //提交的接口
//    private PostApiClaims postApiClaims = new PostApiClaims(new AsyCallBack<PostApiClaims.Info>() {
//        @Override
//        public void onSuccess(String toast, int type, PostApiClaims.Info info) throws Exception {
//
//            result.setVisibility(View.VISIBLE);
//
//        }
//
//        @Override
//        public void onFail(String toast, int type) throws Exception {
//
//            UtilToast.show(getActivity(), toast);
//
//        }
//    });


    //类型联动公司接口
    private GetApiLiPeiSearch getApiLiPeiSearch = new GetApiLiPeiSearch(new AsyCallBack<GetApiLiPeiSearch.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiLiPeiSearch.Info info) throws Exception {

            mDataList.clear();
            companyList.clear();

            mDataList.addAll(info.dataList);

            for (int i = 0; i < mDataList.size(); i++) {
                companyList.add(mDataList.get(i).title);
            }

            company_tv.setText(mDataList.get(0).title);
            company_tv.setTag(mDataList.get(0).id);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(getActivity(), toast);

        }
    });

    //执行查询操作的接口

    private GetApiLiPeiResult getApiLiPeiResult = new GetApiLiPeiResult(new AsyCallBack<GetApiLiPeiResult.SearchInfo>() {

        @Override
        public void onSuccess(String toast, int type, GetApiLiPeiResult.SearchInfo searchInfo) throws Exception {

            Bundle bundle = new Bundle();

            bundle.putSerializable("entityList", searchInfo);

            if (getApiLiPeiSearch.type.equals("1")) {

                startActivity(new Intent(getActivity(), InsuranceSearchCarResultActivity.class).putExtras(bundle));

            } else {

                startActivity(new Intent(getActivity(), InsuranceSearchLifeResultActivity.class).putExtras(bundle));

            }

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, getContext());

        }
    });

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = BoundViewHelper.boundView(this, BaseApplication.ScaleScreenHelper.loadView((ViewGroup) inflater.inflate(R.layout.fragment_lipeichaxun, null)));


        tpyeList.add("车险理赔查询");
        tpyeList.add("寿险理赔查询");
        search_type_tv.setText(tpyeList.get(0));
        driver_rl.setVisibility(View.VISIBLE);


        getApiLiPeiSearch.type = PrefrenceConfig.ONE;
        getApiLiPeiSearch.execute(getActivity());


        search_bt.setOnClickListener(this);
        company_tv.setOnClickListener(this);
        search_type_tv.setOnClickListener(this);

        return view;

    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.search_type_tv:
                showPopWindow(view);
                return;

            case R.id.company_tv:
                showPopWindowCompany(view);
                return;

            case R.id.search_bt:


                String baodanStr = baodan_num_et.getText().toString().replaceAll(" ", "");
                String idCardStr = idcard_num_et.getText().toString().replaceAll(" ", "");
                String nameStr = name_et.getText().toString().replaceAll(" ", "");
                String phoneStr = phone_num_et.getText().toString().replaceAll(" ", "");
                String driverStr = driver_num_et.getText().toString().replaceAll(" ", "");
                String searchTypeStr = search_type_tv.getText().toString();


                if (!BaseApplication.BasePreferences.readIsLogin()) {

                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;

                }

                if (TextUtils.isEmpty(baodanStr)) {
                    UtilToast.show(getActivity(), "保单号不能为空");
                    return;
                }

                if (TextUtils.isEmpty(idCardStr)) {
                    UtilToast.show(getActivity(), "被保人身份证号不能为空");
                    return;
                }

                if (!Validator.isIDCard(idCardStr)) {

                    MyToast.showToast("身份证号格式不正确", getActivity());
                    return;
                }

                if (TextUtils.isEmpty(nameStr)) {
                    UtilToast.show(getActivity(), "被保人姓名不能为空");
                    return;
                }

                if (TextUtils.isEmpty(phoneStr)) {

                    MyToast.showToast("手机号码不能为空", getContext());
                    return;
                }

                if (!Validator.isMobile(phoneStr)) {

                    MyToast.showToast("请输入正确手机号码", getContext());
                    return;
                }

                if (searchTypeStr.equals("车险理赔查询")) {

                    getApiLiPeiResult.cid = company_tv.getTag().toString().replaceAll(" ", "");
                    getApiLiPeiResult.tid = PrefrenceConfig.ONE;
                    getApiLiPeiResult.idnum = idCardStr;
                    getApiLiPeiResult.policynum = baodanStr;
                    getApiLiPeiResult.name = nameStr;
                    getApiLiPeiResult.phone = phoneStr;

                    getApiLiPeiResult.vehicle_license = driverStr;

                    getApiLiPeiResult.userid = BaseApplication.BasePreferences.readUserId();
                    getApiLiPeiResult.execute(getActivity());


                } else {

                    getApiLiPeiResult.cid = company_tv.getTag().toString().replaceAll(" ", "");
                    getApiLiPeiResult.tid = PrefrenceConfig.TWO;
                    getApiLiPeiResult.idnum = idCardStr;
                    getApiLiPeiResult.policynum = baodanStr;
                    getApiLiPeiResult.name = nameStr;
                    getApiLiPeiResult.phone = phoneStr;

                    getApiLiPeiResult.vehicle_license = "";


                    getApiLiPeiResult.userid = BaseApplication.BasePreferences.readUserId();
                    getApiLiPeiResult.execute(getActivity());

                }
                break;

        }

    }


    private void showPopWindow(View view) {

        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.pop_layout, null);

        ListView popList = ((ListView) contentView.findViewById(R.id.item_popList));

        ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), R.layout.support_simple_spinner_dropdown_item, tpyeList);

        popList.setAdapter(adapter);

        final PopupWindow popupWindow = new PopupWindow(contentView, company_tv.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });

        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_white_bg));
        popupWindow.showAsDropDown(view);

        popList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                search_type_tv.setText(tpyeList.get(position));//TODO

                if (position == 0) {
                    getApiLiPeiSearch.type = PrefrenceConfig.ONE;
                    getApiLiPeiSearch.execute(getActivity());
                    driver_rl.setVisibility(View.VISIBLE);
                } else {
                    getApiLiPeiSearch.type = PrefrenceConfig.TWO;
                    getApiLiPeiSearch.execute(getActivity());
                    driver_rl.setVisibility(View.GONE);
                }
                popupWindow.dismiss();
            }
        });
    }

    private void showPopWindowCompany(View view) {

        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.pop_layout, null);

        ListView popList = ((ListView) contentView.findViewById(R.id.item_popList));

        ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), R.layout.support_simple_spinner_dropdown_item, companyList);

        popList.setAdapter(adapter);

        final PopupWindow popupWindow = new PopupWindow(contentView, company_tv.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });

        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_white_bg));
        popupWindow.showAsDropDown(view);

        popList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                company_tv.setText(companyList.get(position));
                company_tv.setTag(mDataList.get(position).id);
                popupWindow.dismiss();
            }
        });
    }


}
